﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbClasses;
using cecbData;
using cecbClasses.Data_Classes;
using cecbClasses.Data_Adapters.Project;

namespace CECB.Project
{
    /// <summary>
    /// Interaction logic for ProjectTypes.xaml
    /// </summary>
    public partial class ProjectTypes : Window
    {
        Data_ProjectTypes ObjProjTyps = new Data_ProjectTypes();
        Func_Projects ObjProjFunc = new Func_Projects();


        public ProjectTypes()
        {

            InitializeComponent();
            ProjTypeAutocompleteTextBox.ItemsSource = ObjProjFunc.GetAllProjectTypes();
        }



        private void button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void ProjTypeAutocompleteTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ObjProjTyps.Ptype_name = ProjTypeAutocompleteTextBox.Text;
            cecbData.ProjectTypes selectedPrj = ObjProjFunc.GetProjectByTypeName(ObjProjTyps);
            label_ProjTypeRefGeneratd.Content = selectedPrj.ptype_reference;
        }

        private void ButtonInsertPage_Click(object sender, RoutedEventArgs e)
        {
            ////wrapPanel_SearchDetails.Visibility = System.Windows.Visibility.Hidden;
            ////wrapPanel_UpdateTypeName.Visibility = System.Windows.Visibility.Hidden;
            ////button_ProjTypeUpdate.Visibility = System.Windows.Visibility.Hidden;
            ////button_ProjTypeDelete.Visibility = System.Windows.Visibility.Hidden;

            //////wrapPanel_EnterTypeRef.Visibility = System.Windows.Visibility.Visible;
            ////wrapPanel_EnterNewType.Visibility = System.Windows.Visibility.Visible;

            ////button_ProjTypeInsert.Visibility = System.Windows.Visibility.Visible;

        }

        private void button_updtDelPage_Click(object sender, RoutedEventArgs e)
        {


            ////wrapPanel_SearchDetails.Visibility = System.Windows.Visibility.Visible;
            ////wrapPanel_UpdateTypeName.Visibility = System.Windows.Visibility.Visible;
            ////button_ProjTypeUpdate.Visibility = System.Windows.Visibility.Visible;
            ////button_ProjTypeDelete.Visibility = System.Windows.Visibility.Visible;

            //////wrapPanel_EnterTypeRef.Visibility = System.Windows.Visibility.Hidden;
            ////wrapPanel_EnterNewType.Visibility = System.Windows.Visibility.Hidden;
        }

        private void button_ProjTypeclose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button_ProjTypeUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ObjProjTyps.Ptype_reference = label_ProjTypeRefGeneratd.Content.ToString();
                ObjProjTyps.Ptype_name = textBox_EnterPrjType.Text;

                ObjProjFunc.UpdateProjTypes(ObjProjTyps);
                MessageBox.Show("Project Type Name Updated Successfully!!", "Updated Information", MessageBoxButton.OK, MessageBoxImage.Information);
                label_ProjTypeRefGeneratd.Content = " ";
                ProjTypeAutocompleteTextBox.Text = " ";
                textBox_EnterPrjType.Text = " ";


            }

            catch
            {
                MessageBox.Show("Project type updation failed,Please select a project type to upadate", "Updated Information", MessageBoxButton.OK, MessageBoxImage.Warning);
                textBox_EntrNewProjType.Text = "";
            }
        }

        private void button_ProjTypeDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are You sure you want to Delete this Project Data", "Delete Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                bool isDeleted = false;


                ObjProjTyps.Ptype_reference = label_ProjTypeRefGeneratd.Content.ToString();
                isDeleted = ObjProjFunc.DeleteProjectTypes(ObjProjTyps);
                if (isDeleted == true)
                {

                    MessageBox.Show("Project Type successfully deleted", "CECB", MessageBoxButton.OK, MessageBoxImage.Information);
                    ProjTypeAutocompleteTextBox.Text = "";



                }
                else
                {
                    MessageBox.Show("Project type delition Failed", "CECB", MessageBoxButton.OK, MessageBoxImage.Warning);

                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // FillProjTy();
            ProjTypeAutocompleteTextBox.ItemsSource = ObjProjFunc.GetAllProjectTypes();

        }

        public void FillProjTy()
        {

            List<cecbData.ProjectTypes> ProjTypeList = ObjProjFunc.GetAllProjectTypes();
            //comboBox_insertProjtypeRef.ItemsSource = ProjTypeList;


        }

        private void button_ProjTypeInsert_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                // ObjProjTyps.Ptype_reference = comboBox_insertProjtypeRef.Text;
                ObjProjTyps.Ptype_name = textBox_EntrNewProjType.Text;

                ObjProjFunc.insertIntoProjectTypes(ObjProjTyps);

                MessageBox.Show("Project Type Inserted SuccessFully!", "Project Types", MessageBoxButton.OK, MessageBoxImage.Information);
                label_ProjTypeRefGeneratd.Content = "";
                textBox_EntrNewProjType.Text = "";
                ProjTypeAutocompleteTextBox.ItemsSource = ObjProjFunc.GetAllProjectTypes();



            }
            catch
            {

                MessageBox.Show("Projet Type Insertion Failed!", "Project Types", MessageBoxButton.OK, MessageBoxImage.Warning);


            }

        }

        private void textBox_EntrNewProjType_TextChanged(object sender, TextChangedEventArgs e)
        {
            button_ProjTypeInsert.IsEnabled = true;
            button_ProjTypeUpdate.IsEnabled = false;
            button_ProjTypeDelete.IsEnabled = false;
        }

        private void ProjTypeAutocompleteTextBox_TextChanged(object sender, RoutedEventArgs e)
        {

            button_ProjTypeInsert.IsEnabled = false;
            button_ProjTypeUpdate.IsEnabled = false;
            button_ProjTypeDelete.IsEnabled = true;

        }

        private void textBox_EnterPrjType_TextChanged(object sender, TextChangedEventArgs e)
        {

            if (label_ProjTypeRefGeneratd.Content == " ")
            {
                //button_ProjTypeUpdate.IsEnabled = false;
                MessageBox.Show("Select a project type to update", "Message", MessageBoxButton.OK, MessageBoxImage.Warning);
                // textBox_EnterPrjType.IsReadOnly = true;
            }
            else
            {
                textBox_EnterPrjType.IsReadOnly = false;
                button_ProjTypeInsert.IsEnabled = false;
                button_ProjTypeUpdate.IsEnabled = true;
                button_ProjTypeDelete.IsEnabled = false;
            }
        }



    }
}
